package com.ant.collect.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ant.collect.common.ResultVo;
import com.ant.collect.dto.UserInfoDto;
import com.ant.collect.handler.AntException;
import com.ant.collect.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "登录接口")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    IUserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping()
    public ResultVo login(@RequestBody UserInfoDto dto, HttpServletRequest request) throws AntException {

        // 1. 获取前端提交的验证码
//        String inputCaptcha = dto.getCaptcha();
//        if (inputCaptcha == null || inputCaptcha.isEmpty()) {
//            return ResultVo.fail("请输入验证码");
//        }
//
//        // 2. 获取Session中存储的验证码
//        HttpSession session = request.getSession();
//        String sessionCaptcha = (String) session.getAttribute("CAPTCHA_CODE");
//        if (sessionCaptcha == null) {
//            return ResultVo.fail("验证码已过期，请刷新");
//        }
//
//        // 3. 验证（忽略大小写）
//        if (!inputCaptcha.equalsIgnoreCase(sessionCaptcha)) {
//            return ResultVo.fail("验证码错误");
//        }
        Map<String,String> token = userService.selectUserPwd(dto);
        return ResultVo.success("登录成功", token);
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public ResultVo register(@RequestBody UserInfoDto dto) throws Exception {
          userService.saveUserMsg(dto.getUsername(), dto.getPassword());
        return ResultVo.success("注册成功");
    }

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    public ResultVo test() {
        return ResultVo.success("请求成功",StpUtil.getLoginId());
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/loginOut")
    public ResultVo loginOut() {
        StpUtil.logout();
        return ResultVo.success("请求成功");
    }
    // 强制踢人接口（管理员使用）
    @GetMapping("/kickout/{userId}")
    public ResultVo kickout(@PathVariable Long userId) {
        StpUtil.kickout(userId); // 强制该用户所有设备下线
        return ResultVo.success("已强制下线");
    }



//    @ApiOperation(value = "压缩包上传")
//    @PostMapping ("/zipUpload")
//    public ResultVo zipUpload(MultipartFile file) throws Exception {
//        return new ResultVo(userService.zipUploades(file));
//    }


}
